<?php

/**
 * ExtPanelBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
class ExtPanelBase extends ExtContainer
{
    public $hasHtmlElements = false;

    public function parseNode(ExtHTMLParserNode $node, ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);

        if ($this->parseAutoLoad($node, $astwalker))
        {
            $this->parseTopBar($node, $astwalker);
            $this->parseBottomBar($node, $astwalker);
            $this->parseFooterBar($node, $astwalker);
            $this->parseButtons($node, $astwalker);
        }
        else
        {
            $this->parseBody($node, $astwalker);
            $this->parseItems($node, $astwalker);
            $this->parseTopBar($node, $astwalker);
            $this->parseBottomBar($node, $astwalker);
            $this->parseFooterBar($node, $astwalker);
            $this->parseButtons($node, $astwalker);
        }
    }

    private function parseAutoLoad($node, $astwalker)
    {
        $autoload = $node->findPropertyByName('autoload');
        if (!is_null($autoload))
        {
            $items = $this->parseNodeWidthTag($autoload,
                            $astwalker,
                            true,
                            null,
                            $this);
            $autoload->shiftUp();
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Is also called internally by parseBody when there are not html elements there
     * @param <type> $node
     * @param <type> $astwalker
     * @param <type> $propname
     */
    private function parseItems($node, $astwalker)
    {
        $pitems = $node->findPropertyByName('items');
        if (!is_null($pitems))
        {
            $items = parent::parseNodeWidthTag($pitems,
                            $astwalker,
                            false,
                            array('asobject' => false),
                            $this);
            $this->setArrayConfig('items', $items);
            $pitems->shiftUp();
        }
    }

    protected function parseBody($node, $astwalker, $propname='body')
    {
        $body = $node->findPropertyByName($propname);
        if (!is_null($body))
        {
            /*
             * check if the first item is a layout container then we add
             * the inner controls hosted in the layout into the Panel items
             * as lazy object.
             */
            if (count($body->children) != 0 && ($body->firstChild()->extctl instanceof ExtContainerLayout))
            {
                $items = parent::parseNodeWidthTag($body,
                                $astwalker,
                                false,
                                array('asobject' => false),
                                $this);
                $this->setArrayConfig('items', $items);
            }
            else
            {
                /*
                 * if defaults is defined for this panel, then the nested
                 * controls are added as items insted of individual objects
                 * that are rendered into into the main object tree
                 */
                if ($this->has_defaults)
                {
                    $items = parent::parseNodeWidthTag($body,
                                    $astwalker,
                                    false,
                                    array('asobject' => false),
                                    $this);
                    $this->setArrayConfig('items', $items);
                }
                else
                {
                    /**
                     * Treat items as if the body tag doesn't have html element
                     */
                    if (!$this->hasHtmlChildren($body))
                    {
                        $items = parent::parseNodeWidthTag($body,
                                        $astwalker,
                                        false,
                                        array('asobject' => false),
                                        $this);
                        $this->setArrayConfig('items', $items);
                    }
                    else
                    {
                        $items = parent::parseNodeWidthTag($body,
                                        $astwalker,
                                        false,
                                        null,
                                        $this);
                        foreach ($items as $item)
                            $this->getController()->addExtJs($item);
                    }
                }
            }

            /*
             * Always create body containers since it is not efficiently to know
             * whther a child element is a panel and has h5tml elements.
             */

            if (!$astwalker->getOption('disableContentElement', false))
            {
                if ($this->hasHtmlElements)
                {
                    $body->attr = array();
                    $body->tag = 'div';
                    $body_el = $this->getId() . '_body';
                    $body->setAttribute('id', $body_el);
                    $body->setClass('x-hidden');
                    $this->setConfig('contentEl', $body_el);
                }
                else
                {
                    $body->shiftUp();
                }
            }
            else
            {
                $body->shiftUp();
            }
        }
    }

    protected function hasHtmlChildren($node)
    {
        foreach ($node->nodes as $item)
        {
            //anything except property or component
            if ( (($item->nodetype == HDOM_TYPE_ELEMENT || $item->nodetype == HDOM_TYPE_TEXT) && trim($item->outertext()) != '')
                 || ($item->extctl != null && $item->extctl->isHTMLElement()) )
            {
                $this->hasHtmlElements = true;
                return true;
            }
        }
        $this->hasHtmlElements = false;
        return $this->hasHtmlElements;
    }

    private function parseTopBar($node, $astwalker)
    {
        $tbar = $node->findPropertyByName('toptoolbar');
        if (!is_null($tbar))
        {
            $items = parent::parseNodeWidthTag($tbar,
                            $astwalker,
                            true,
                            null,
                            $this);
            $this->setJavaScriptConfig('tbar', $items[0]);
        }
    }

    private function parseBottomBar($node, $astwalker)
    {
        $bbar = $node->findPropertyByName('bottomtoolbar');
        if (!is_null($bbar))
        {
            $items = parent::parseNodeWidthTag($bbar,
                            $astwalker,
                            true,
                            null,
                            $this);
            $this->setJavaScriptConfig('bbar', $items[0]);
        }
    }

    private function parseFooterBar($node, $astwalker)
    {
        $fbar = $node->findPropertyByName('footertoolbar');
        if (!is_null($fbar))
        {
            $items = parent::parseNodeWidthTag($fbar,
                            $astwalker,
                            true,
                            null,
                            $this);
            $this->setJavaScriptConfig('fbar', $items[0]);
        }
    }

    private function parseButtons($node, $astwalker)
    {
        $buttons = $node->findPropertyByName('buttons');
        if (!is_null($buttons))
        {
            $items = parent::parseNodeWidthTag($buttons,
                            $astwalker,
                            true,
                            array('asobject' => false),
                            $this);
            $this->setArrayConfig('buttons', $items);
        }
    }

    public function createContainer(ExtHTMLParserNode $node)
    {
        if ($this->parent instanceof ExtBorderLayout_RegionBase)
        {
            $node->shiftUp();
            return;
        }
        else if ($this->parent instanceof ExtTabPanelBase)
        {
            $node->shiftUp();
            return;
        }
        else if ($this->parent instanceof ExtMenuBase)
        {
            $node->shiftUp();
            return;
        }
        else
        {
            parent::createContainer($node);
        }
    }
}
?>
